﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SCS.DAL;
using SCS.Common.Models;
using SCS.Common;


namespace SCS.Business
{
    public class PrivateMessageService
    {
        IUnitOfWork _unitOfWork;
        public PrivateMessageService()
        {
            _unitOfWork = new UnitOfWork();
        }
        public MembershipService GetMembershipService()
        {
            return new MembershipService(_unitOfWork.GetContext());
        }
        public PrivateMessage SanitizeMessage(PrivateMessage privateMessage)
        {
            privateMessage.message = StringUtils.GetSafeHtml(privateMessage.message);
            privateMessage.subject = StringUtils.SafePlainText(privateMessage.subject);
            return privateMessage;
        }

        /// <summary>
        /// Add a private message
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public PrivateMessage Add(PrivateMessage message)
        {
            message = SanitizeMessage(message);
            message.dateSent = DateTime.UtcNow;
            message.isReceiverDeleted = false;
            message.isSenderDeleted = false;
            message.messageID = _unitOfWork.PrivateMessage.GetAll().Count();
            _unitOfWork.PrivateMessage.Add(message);
            _unitOfWork.Commit();
            return message;
        }

        /// <summary>
        /// Return a private message by Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public PrivateMessage Get(int id)
        {
            return _unitOfWork.PrivateMessage.Get(m => m.messageID == id).ToList().FirstOrDefault();
        }

        /// <summary>
        /// GetLastMessageID
        /// </summary>
        /// <param name="userName">userName</param>
        /// <returns>last message id</returns>
        public int GetLastMessageID(string userName)
        {
            return _unitOfWork.PrivateMessage.GetLastReceiceID(userName);
        }

        /// <summary>
        /// GetUserName
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>user</returns>
        public MembershipUser GetUserName(int? id)
        {
            return _unitOfWork.MembershipUser.GetAll().Where(m => m.userID == id).FirstOrDefault();
        }

        /// <summary>
        /// Save a private message
        /// </summary>
        /// <param name="message"></param>
        public void Save(PrivateMessage message)
        {
            message = SanitizeMessage(message);
            _unitOfWork.PrivateMessage.Update(message);
            _unitOfWork.Commit();
        }

        /// <summary>
        /// Return list of paged private messages by sent user
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public IPagedList<PrivateMessage> GetPagedSentMessagesByUser(int pageIndex, int pageSize, MembershipUser user)
        {
            return _unitOfWork.PrivateMessage.GetPagedSentMessagesByUser(pageIndex, pageSize, user);
        }

        /// <summary>
        /// Return list of paged private messages by received user
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public IPagedList<PrivateMessage> GetPagedReceivedMessagesByUser(int pageIndex, int pageSize, MembershipUser user)
        {
            return _unitOfWork.PrivateMessage.GetPagedReceivedMessagesByUser(pageIndex, pageSize, user);
        }

        /// <summary>
        /// Gets the last sent private message from a specific user
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public PrivateMessage GetLastSentPrivateMessage(int Id)
        {
            return _unitOfWork.PrivateMessage.GetLastSentPrivateMessage(Id);
        }

        /// <summary>
        /// Gets all private messages sent by a user
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IList<PrivateMessage> GetAllSentByUser(int Id)
        {
            return _unitOfWork.PrivateMessage.GetAllSentByUser(Id);
        }

        /// <summary>
        /// Returns a count of any new messages the user has
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public int NewPrivateMessageCount(string userName)
        {
            return _unitOfWork.PrivateMessage.NewPrivateMessageCount(userName);
        }

        /// <summary>
        /// Gets all private messages received by a user
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IList<PrivateMessage> GetAllReceivedByUser(int Id)
        {
            return _unitOfWork.PrivateMessage.GetAllReceivedByUser(Id);
        }

        /// <summary>
        /// get all private messages sent from one user to another
        /// </summary>
        /// <param name="senderId"></param>
        /// <param name="receiverId"></param>
        /// <returns></returns>
        public IList<PrivateMessage> GetAllByUserToAnotherUser(int senderId, int receiverId)
        {
            return _unitOfWork.PrivateMessage.GetAllByUserToAnotherUser(senderId, receiverId);
        }

        /// <summary>
        /// Delete a private message
        /// </summary>
        /// <param name="message"></param>
        public void DeleteMessage(PrivateMessage message,bool isSenderUser)
        {
            if (isSenderUser)
            {
                message.isSenderDeleted = true;
            }
            else
            {
                message.isReceiverDeleted = true;
            }
            if (message.isReceiverDeleted && message.isSenderDeleted)
            {
                _unitOfWork.PrivateMessage.Delete(message);
                _unitOfWork.Commit();
            }
            else
            {
                _unitOfWork.PrivateMessage.Update(message);
                _unitOfWork.Commit();
            }
        }
    }
}
